package com.climb.moive.controller;

import com.climb.moive.entity.Member;
import com.climb.moive.entity.MovieEvaluation;
import com.climb.moive.entity.MovieReview;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.MovieEvalService;
import com.climb.moive.service.MovieReviewService;
import com.climb.moive.service.MovieService;
import com.climb.moive.service.impl.MoiveEvalServiceImpl;
import com.climb.moive.service.impl.MovieReviewServiceImpl;
import com.climb.moive.service.impl.MovieServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Administrator
 * 会员-电影表现层
 */
@WebServlet("/memberMovie")
public class MemberMovieController extends BaseController {
    MovieService movieService = new MovieServiceImpl();
    MovieReviewService reviewService = new MovieReviewServiceImpl();
    MovieEvalService evalService = new MoiveEvalServiceImpl();

    /**
     * 跳转到会员-电影详情页面
     */
    @Override
    public String index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("member") != null) {

            /*获取id*/
            Integer id = Integer.valueOf(req.getParameter("id"));
            /*查找电影movie返回电影详情页面*/
            req.setAttribute("movie", movieService.findMovieById(id));
            /*查找reviewEssayList影评文章并返回*/
            req.setAttribute("reviewEssayList", reviewService.findReviewByMoiveid(id));
            /*查找影评短评evaluationList并返回*/
            req.setAttribute("evaluationList", evalService.findEvaByMovieid(id));
            return "forward:/view/member/MemberMovieDetail.jsp";
        } else {
            return "redirect:"+req.getContextPath()+"/memberIndex?method=doLogin";
        }
    }

    /**
     * 会员-查看电影评论文章详情
     */
    public String viewReview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        /*获取id*/
        Integer id = Integer.valueOf(req.getParameter("id"));
        /*查找reviewEssay影评文章并返回*/
        req.setAttribute("reviewEssay", reviewService.findReviewByRid(id));
        return "forward:/view/member/MemberMovieReview.jsp";


    }

    /**
     * 会员-发表短评跳转
     */
    public String doEvaluation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取电影id*/
        Integer id = Integer.valueOf(req.getParameter("id"));
        /*查找电影文章并返回*/
        req.setAttribute("movie", movieService.findMovieById(id));
        return "forward:/view/member/MemberPublishMovieEvaluation.jsp";
    }

    /**
     * 会员-发表文章跳转
     */
    public String doReview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取电影id*/
        Integer id = Integer.valueOf(req.getParameter("id"));
        /*查找电影影评文章并返回*/
        req.setAttribute("movie", movieService.findMovieById(id));
        return "forward:/view/member/MemberPublishMovieReview.jsp";
    }

    /**
     * 会员影评处理
     */
    public String evaluation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取电影id*/
        Integer movId = Integer.valueOf(req.getParameter("movId"));
        String content =java.net.URLDecoder.decode(req.getParameter("content")) ;
        System.out.println(content);
        BigDecimal score = new BigDecimal(req.getParameter("score"));
        HttpSession session = req.getSession();
        Member member = (Member) session.getAttribute("member");
        MovieEvaluation movieEvaluation=new MovieEvaluation(movId,member.getId(),score,content,member.getName(),new Date());
        /*存入数据库并返回*/
        evalService.saveEvaluation(movieEvaluation);
        return "forward:/view/member/MemberPublishMovieEvaluation.jsp";
    }
    /**
     * 会员影评文章处理
     */
    public String review(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取电影id*/
        Integer movId = Integer.valueOf(req.getParameter("movId"));
        String content =java.net.URLDecoder.decode(req.getParameter("content")) ;
        String title=java.net.URLDecoder.decode(req.getParameter("title")) ;
        BigDecimal score = new BigDecimal(req.getParameter("score"));
        HttpSession session = req.getSession();
        Member member = (Member) session.getAttribute("member");
        MovieReview movieReview=new MovieReview(movId,member.getId(),score,title,content,member.getName(),new Date());
        /*存入数据库并返回*/
        reviewService.saveMovieReview(movieReview);
        return "forward:/view/member/MemberPublishMovieReview.jsp";

    }
}
